﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using TOOLS;
namespace BLL
{
    public  class MUserCenter
    {
        public static tb_m_user Login(string username, string password)
        {
            var dalMUser = new DAL.MUser();
            return dalMUser.GetOne(p=>p.username==username&&p.password==password);
        }
        public static tb_m_user Login(string username, string password, out string loginKey)
        {
            var dalMUser = new DAL.MUser();
            Model.tb_m_user loginUser = dalMUser.GetOne(p => username == p.username && password == p.password);

            if (loginUser != null)
            {
                var temp =BaseBll.mLoginUserList.Where(p => p.Value.loginUser.uid == loginUser.uid && p.Value.flag).FirstOrDefault();

                if (temp.Key != null)
                {
                    BaseBll.EnLog(string.Format("后台用户名:\"{0}\"多点登录，被强制下线", username));
                    BaseBll.mLoginUserList[temp.Key].flag = false;//强制下线
                }
                loginKey = Guid.NewGuid().ToString();
                BaseBll.mLoginUserList.Add(loginKey, new MLoginInfo()
                {
                    endTime = DateTime.Now,
                    startTime = DateTime.Now,
                    loginUser = loginUser,
                    loginKey = loginKey,
                    flag = true
                });
                BaseBll.EnLog(string.Format("后台用户名:\"{0}\"登录", username));
            }
            else
            {
                loginKey = "";
            }

            return loginUser;
        }
        public static string Heartbeat(string loginKey, out tb_m_user userEntity)
        {

            userEntity = null;
            if (BaseBll.mLoginUserList.ContainsKey(loginKey))
            {
                if (BaseBll.mLoginUserList[loginKey].flag)
                {
                    BaseBll.mLoginUserList[loginKey].endTime = DateTime.Now;
                    userEntity = BaseBll.mLoginUserList[loginKey].loginUser;
                    var oldLoginEntity = BaseBll.mLoginUserList.Where(p => BaseBll.mLoginUserList[loginKey].loginUser.uid == p.Value.loginUser.uid && !p.Value.flag).ToList();
                    if (oldLoginEntity != null)
                    {
                        foreach (var item in oldLoginEntity)
                        {
                            BaseBll.mLoginUserList.Remove(item.Key);
                            BaseBll.EnLog(string.Format("uid:{0} username:{1}清除残留登录信息", BaseBll.mLoginUserList[loginKey].loginUser.uid, BaseBll.mLoginUserList[loginKey].loginUser.username));
                        }
                    }
                    return "";
                }
                else
                {
                    BaseBll.mLoginUserList.Remove(loginKey);
                    return  "帐号在其它地点登录，被强迫下线";
                }
            }
            else
            {
                return "登录超时";
            }
        }
    }
}
